<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\OrderCommissions;
use Common\Models\Orders;
use Common\Models\User;
use Services\Api\OrderServices;

class OrderController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}


    public function search_typeAction(){
        $time=[
            ['type'=>'today','name'=>'今日'],
            ['type'=>'yesterday','name'=>'昨日'],
            ['type'=>'three_day','name'=>'三天'],
            ['type'=>'week','name'=>'一星期'],
            ['type'=>'this_month','name'=>'本月'],
            ['type'=>'last_month','name'=>'上月'],
            ['type'=>'three_month','name'=>'近三月'],
            ['type'=>'six_month','name'=>'半年'],
            ['type'=>'this_year','name'=>'本年'],
        ];
        $site = [
            ['type'=>'haodanku','name'=>'淘宝'],
            ['type'=>'elm','name'=>'饿了么'],
            ['type'=>'koubei','name'=>'口碑'],
            ['type'=>'jd','name'=>'京东'],
            ['type'=>'pdd','name'=>'拼多多'],
            ['type'=>'vph','name'=>'唯品会'],
            ['type'=>'duomai','name'=>'综合'],
        ];
        $result['time'] = $time;
        $result['site'] = $site;
        $this->ok($result);
    }


    /**
     * @func 列表
     */
	public function order_listAction(){
		$data = [];
		$params=$this->params;
		$data['user_id'] = $params['user_id'];
		$data['source'] = $params['source'];
		if(array_key_exists('attr',$params)  && !empty($params['attr']) ){
		    $data['is_free']=$params['attr'];
		}
		if(array_key_exists('site',$params)  && !empty($params['site']) ){
		    $data['site']=$params['site'];
		}
		if(array_key_exists('date',$params)  && !empty($params['date'])){
		    switch ($params['date']){
                case 'today':
                    $data['date'] =Common::timeChange(1);
                    break;
                case 'yesterday':
                    $data['date'] = Common::timeChange(2);
                    break;
                case 'three_day':
                    $data['date'] = Common::timeChange(11);
                    break;
                case 'week':
                    $data['date'] = Common::timeChange(14);
                    break;
                case 'this_month':
                    $data['date'] = Common::timeChange(8);
                    break;
                case 'last_month':
                    $data['date'] = Common::timeChange(9);
                    break;
                case 'three_month':
                    $data['date'] = Common::timeChange(7);
                    break;
                case 'six_month':
                    $data['date'] = Common::timeChange(12);
                    break;
                case 'this_year':
                    $data['date'] = Common::timeChange(13);
                    break;
            }
		}

        if(isset($params['commission_type']) && !empty($params['commission_type'])){
            $data['commission_type'] = $params['commission_type'];
        }else{
            $data['commission_type']=1;
        }

        //当前页数
        if(!isset($this->params['page']) || empty($this->params['page'])){
            $page = 1;
        }else{
            $page = (int)$this->params['page'];
        }

        //当前条数
        if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
            $pageSize = 10;
        }else{
            $pageSize = (int)$this->params['page_size'];
        }

        if(isset($params['status']) && !empty($params['status'])){
            $data['status']=$params['status'];
        }

        if(isset($params['site']) && !empty($params['site'])){
            $data['site']=$params['site'];
        }
        $orderServices = new OrderServices();
		$list_items = $orderServices->order_list($data,$page,$pageSize);
        $this->ok($list_items);
	}
	/*
	 找回订单
	 */
	public function find_back_orderAction(){
		$order=new Orders();
		$order_info=$order->find_back_order($this->params['order_id'],$this->params['user_id'],$this->params['type']);
		if($order_info != false){
			$order_info['goods_pic']=str_replace('//img','http://img',$order_info['goods_pic']);
			switch($order_info['site']){
			case 'haodanku':$order_info['ShopType']='淘宝';break;
			case 'jd':$order_info['ShopType']='京东';break;
			case 'pdd':$order_info['ShopType']='拼多多';break;
			}
		$this->ok($order_info);
		}
		$this->ok([],2);
	
	}



    /**
     * 我的收益数据结构
     */
    public function order_extendsAction(){
        $start_time_end=Common::timeChange(1);
        $result['today']=$this->order_survey($start_time_end);
        $start_time_end=Common::timeChange(2);
        $result['yestoday']=$this->order_survey($start_time_end);

        $this_month_time=Common::timeChange(8);
        $orderCommission = new OrderCommissions();
        $result['this_month']=round($orderCommission->getOrderSettlementCommission($this->user_info['id'],$this_month_time),2);
        $up_month_time=Common::timeChange(9);
        $result['up_month']=round($orderCommission->getOrderSettlementCommission($this->user_info['id'],$up_month_time),2);
        $result['all_month']=round($orderCommission->all_pre_no_goods($this->user_info['id']),2);
        $settled_time_end['start_at']="2019-10-01 00:00:00";
        $settled_time_end['end_at']="2222-10-01 00:00:00";
        $result['settlement_all'] = round($orderCommission->getOrderSettlementCommission($this->user_info['id'],$settled_time_end),2);
        $this->ok($result);
    }

    /**
     * 我的更多收入
     * @param
     * @param int $path
     * @return mixed
     */
    public function order_moreAction(){
        if((!isset($this->params['start_at']) || empty($this->params['start_at'])) && (!isset($this->params['end_at']) || empty($this->params['end_at']))){
            $start_time_end=Common::timeChange(8);
            $result['month']=$this->order_survey($start_time_end);
            $result['choosetime']=0;
        }else{
            if(!isset($this->params['start_at']) || empty($this->params['start_at'])){
                $this->fail(-1,'请输入开始日期');
            }
            if(!isset($this->params['end_at']) || empty($this->params['end_at'])){
                $this->fail(-1,'请输入结束日期');
            }
            $start_at = Common::valid_date($this->params['start_at']);
            if(!$start_at){
                $this->fail(-1,'开始时间日期格式不正确');
            }

            $start_time_end['start_at']=$this->params['start_at'];
            $end_at = Common::valid_date($this->params['end_at']);
            if(!$end_at){
                $this->fail(-1,'结束时间日期格式不正确');
            }
            $start_time_end['end_at']=$this->params['end_at'];
            $result['month']=0;
            $result['choosetime']=$this->order_survey($start_time_end);
        }
        $this->ok($result);
    }

    public function order_survey($start_time_end,$path=0){
        $orderCommission = new OrderCommissions();

        $settlement['order_status']='6';
        $settlement['buyer_id']=$this->user_info['id'];
        $result['settlement_order_money'] = round($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$settlement),2);    //结算订单结算-自购
        $settlement['buyer_id']=$path;
        $result['settlement_money']=round($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$settlement),2);//结算总和
        $result['settlement_team_money'] = ($result['settlement_money']-$result['settlement_order_money'])>=0 ? round(($result['settlement_money']-$result['settlement_order_money']),2) : 0;    //结算团队

        $estimate['order_status']='2,4,6';
        $estimate['buyer_id']=$this->user_info['id'];
        $result['estimate_order_money'] = round($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate),2);    //预估订单结算-自购

        unset($estimate['buyer_id']);
        $estimate['nobuyer_id']=$this->user_info['id'];
        $result['estimate_team_money'] = round($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate),2);    //预估团队

        unset($estimate['nobuyer_id']);
        $result['estimate_money']=round($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate),2);//预估总和
        $result['order_count']=(int)$orderCommission->order_all($this->user_info['id'],$start_time_end);
        return $result;
    }



    //我的收益
    public function myincomeAction(){
        $start_time_end=Common::timeChange(1);
        $result['today']=$this->myincodedetail($start_time_end);
        $start_time_end=Common::timeChange(2);
        $result['yestoday']=$this->myincodedetail($start_time_end);
        $this_month_time=Common::timeChange(8);
        $orderCommission = new OrderCommissions();
        $result['this_month']=Common::number_randtwo($orderCommission->getOrderSettlementCommission($this->user_info['id'],$this_month_time));    //本月确认收货
        $up_month_time=Common::timeChange(9);
        $result['up_month']=Common::number_randtwo($orderCommission->getOrderSettlementCommission($this->user_info['id'],$up_month_time));        //上月确认收货
        $result['all_month']=Common::number_randtwo($orderCommission->all_pre_no_goods($this->user_info['id']));
        $settled_time_end['start_at']="2019-10-01 00:00:00";
        $settled_time_end['end_at']="2222-10-01 00:00:00";
        $result['settlement_all'] = Common::number_randtwo($orderCommission->getOrderSettlementCommission($this->user_info['id'],$settled_time_end));
        $withdrawals = new Withdrawals();
        $result['withdrawals_money'] = $withdrawals->get_withdrawal_all($this->user_info['id']);//已经提现成功的钱
        $wallets = new Wallets();
        $user_wallets = $wallets->findbyUserId($this->user_info['id']);
        $result['amount'] = isset($user_wallets->amount) ? Common::number_randtwo($user_wallets->amount):'0.00';
        $this->ok($result);
    }




    //我的收益_按时间 查询
    public function myincome_searchAction(){
        if(!isset($this->params['search_time']) && empty($this->params['search_time'])){
            $start_time_end=Common::timeChange(8);
        }else{
            $search_time = explode(',',$this->params['search_time']);
            $start_time_end['start_at']=date('Y-m-d H:i:s',strtotime($search_time[0]));
            $start_time_end['end_at']=date('Y-m-d H:i:s',strtotime($search_time[1])+86399);
        }
        $result=$this->myincodedetail($start_time_end);
        $orderCommission = new OrderCommissions();
        $result['this_month']=Common::number_randtwo($orderCommission->getOrderSettlementCommission($this->user_info['id'],$start_time_end));    //本月确认收货
        $this->ok($result);
    }



    private function myincodedetail($start_time_end,$path=0){
        $orderCommission = new OrderCommissions();
        $settlement['order_status']='6';
        $settlement['buyer_id']=$this->user_info['id'];
        $result['settlement_order_money'] = Common::number_randtwo($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$settlement));    //结算订单结算-自购
        $settlement['buyer_id']=$path;
        $result['settlement_money']=Common::number_randtwo(($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$settlement)));//结算总和
        $result['settlement_team_money'] = ($result['settlement_money']-$result['settlement_order_money'])>=0 ? Common::number_randtwo($result['settlement_money']-$result['settlement_order_money']) : 0;    //结算团队

        $estimate['order_status']='2,4,6';
        $estimate['buyer_id']=$this->user_info['id'];
        $result['estimate_order_money'] = Common::number_randtwo(($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate)));    //预估订单结算-自购
        unset($estimate['buyer_id']);
        $estimate['nobuyer_id']=$this->user_info['id'];
        $result['estimate_team_money'] = Common::number_randtwo($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate));    //预估团队
        unset($estimate['nobuyer_id']);
        $result['estimate_money']=Common::number_randtwo($orderCommission->getOrderCommissions_tj($this->user_info['id'],$start_time_end,$estimate));//预估总和

        $result['order_count']=(int)$orderCommission->order_all($this->user_info['id'],$start_time_end);
        $settlement['buyer_id']=$this->user_info['id'];
        $result['self_order_count']=(int)$orderCommission->order_all($this->user_info['id'],$start_time_end,$settlement);
        $result['team_order_count']=($result['order_count']-$result['self_order_count'])>=0 ? $result['order_count']-$result['self_order_count'] : 0;    //结算团队

        return $result;
    }


    //我的上个月和上上个月的收益
    public function get_month_up_moneyAction(){
        $orderCommission = new OrderCommissions();

        $up_month_time = Common::timeChange(8);
        $month_title = date('Y年m月',strtotime($up_month_time['start_at']));
        $earning_time_tj = $orderCommission->getOrderCommissions_earning_time_tj($this->user_info['id'],$up_month_time);
        $result_order[0]['money']=Common::number_randtwo($earning_time_tj);
        $result_order[0]['title']=$month_title.'订单佣金';
        $predict_money=Common::up_month($up_month_time['start_at']);
        $result_order[0]['predict_money']='预计'.$predict_money.'25日到账';

        $up_up_month_time = Common::timeChange(9);
        $month_title = date('Y年m月',strtotime($up_up_month_time['start_at']));
        $earning_time_tj = $orderCommission->getOrderCommissions_earning_time_tj($this->user_info['id'],$up_up_month_time);
        $result_order[1]['money']=Common::number_randtwo($earning_time_tj);
        $result_order[1]['title']=$month_title.'订单佣金';
        $predict_money=Common::up_month($up_up_month_time['start_at']);
        $result_order[1]['predict_money']='预计'.$predict_money.'25日到账';

        $this->ok($result_order);


    }



}
